#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#define ASC2HEXD(x) (((x) >= '0' && (x) <= '9') ?               \
		     ((x) - '0') : (toupper(x) - 'A' + 10))

#define ISXDIGIT(x) isxdigit ((unsigned char)(x))

static void
decode_string (char *s)
{
  char *p = s;

  for (; *s; s++, p++)
    {
      if (*s != '%')
	*p = *s;
      else
	{
	  /* Do nothing if at the end of the string, or if the chars
	     are not hex-digits.  */
	  if (!*(s + 1) || !*(s + 2)
	      || !(ISXDIGIT (*(s + 1)) && ISXDIGIT (*(s + 2))))
	    {
	      *p = *s;
	      continue;
	    }
	  *p = (ASC2HEXD (*(s + 1)) << 4) + ASC2HEXD (*(s + 2));
	  s += 2;
	}
    }
  *p = '\0';
}

int main(int argc,char **argv)
{
  //  char *p = "http://www.google.com/search?hl=zh-CN&q=http+%2525&lr=";
  //    char *p = "http://hiphotos.baidu.com/%BA%DA%CD%C3%D7%D3%B5%C4%BC%C7%D2%E4/pic/item/8f72b617d571623d972b43f5.jpeg";
  char *p = "http://www.namipan.com/d/%e6%99%9a%e9%a6%99%e7%8e%89%2b-%2b%e7%b6%a0%e5%9e%bb%e5%a8%98%e4%b9%8b%e6%ad%8c.mp3/2b0a322cbee240b04102d2516fc822c9acbf6969de666100";
    /*if (argc <= 1) {
    printf("Usage:decode_string [URL]");
    return 1;
    }*/
  //  char *p = argv[1];
  char *str = (char *) malloc(sizeof(*p));
  strcpy(str,p);
  decode_string(str);
  printf("%s\n",str);
  return 0;
}
